/*
Copyright DAUPHIN Maxime 2005

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
http://www.gnu.org/licenses/gpl.txt
*/
#include "Win32Lib/dc.h"
#include "Win32Lib/Bitmap.h"
#include "Win32Lib/ClipBoard.h"
#include "Item.h"

Item::Item(void)
{
	m_hIconText = ::LoadIcon( g_hInstance, "IDI_TEXT" );
	assert(m_hIconText);
	m_hIconUnknown = ::LoadIcon( g_hInstance, "IDI_UNKNOWN" );
	assert(m_hIconUnknown);
	m_hIconFiles = ::LoadIcon( g_hInstance, "IDI_FILES" );
	assert(m_hIconFiles);
	
	m_colorStart = Gui::Color( COLOR_WHITE );
	m_colorEnd = Gui::Color( COLOR_GREY );
	
}

Item::~Item(void)
{
}

int Item::paint()
{
	Gui::Dc dc(this);
	dc.setBkMode( TRANSPARENT );
	Gui::Rect rect = getClientRect();
	dc.drawGradient( rect, m_colorStart, m_colorEnd );
	
	STRING str = "Empty";

	switch ( m_uFormat )
	{
	case CF_TEXT :
		{
			dc.drawIcon( Gui::Point( 5, 5 ), m_hIconText );
			str = Gui::any_cast<STRING>(m_any);
			break;
		}
	case CF_BITMAP :
		{
			str = _T("bitmap");
			Gui::Bitmap bmp = Gui::any_cast<Gui::Bitmap>(m_any);
			bmp.draw( dc, Gui::Rect( 5, 5, 32, 32 ) );
			break;
		}
	case CF_HDROP :
		{
			str.clear();
			dc.drawIcon( Gui::Point( 5, 5 ), m_hIconFiles );
			Gui::DropFile &drop = Gui::any_cast<Gui::DropFile>(m_any);
			drop.resetIterator();
			STRING fileName = drop.getNextFile();
			while ( fileName != _T("") )
			{
				str += Gui::getShortFileName(fileName);
				str += _T("\r\n");
				fileName = drop.getNextFile();
			}
			break;
		}
	default :
		{
			dc.drawIcon( Gui::Point( 5, 5 ), m_hIconUnknown );
			str = _T("ClipBoard Format unknown");
			break;
		}
	}

	rect.t.x += 40;
	dc.drawText( str, rect, DT_LEFT );

	return 0;
}

void Item::setContant( Gui::Any &any, UINT uFormat )
{
	//m_contant = contant;
	m_any = any;
	m_uFormat = uFormat;
}

int Item::onMouseMove(Gui::Point point)
{
	Gui::Rect rect = getClientRect();
	Gui::Color oldColor = m_colorStart;
	if ( rect.isIn( screenToClient(point) ) )
	{
		//We are in
		m_colorStart = Gui::Color( COLOR_BLUE_START );
		m_colorEnd = Gui::Color( COLOR_BLUE_END );
	}
	else
	{
		m_colorStart = Gui::Color( COLOR_WHITE );
		m_colorEnd = Gui::Color( COLOR_GREY );
	}
	//Update
	if ( oldColor != m_colorStart )
		paint();
	return 0;
}

void Item::setDataClipBoard( Gui::Point point, Gui::ClipBoard *pClip )
{
	assert(pClip);
	Gui::Rect rect = getClientRect();
	if ( rect.isIn( screenToClient(point) ) )
	{
		pClip->setData( m_uFormat, m_any );
	}
}